<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 常用乘车人 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<!-- 乘车人列表 -->
		<view class="container-main">
			<view class="main-list" v-if="passengerList.length">
				<view class="list-item flex align-items-start" v-for="item in passengerList" :key="item.id">
					<view class="item-mask flex-item" v-if="passengerLoading"></view>
					<view class="item-box flex-item flex" v-else>
						<view class="text" v-if="item.name">{{item.name}}</view>
						<view class="text">{{item.mobile}}</view>
					</view>
					<view class="item-operate" :class="{expand: item.openOperate}">
						<image class="operate-btn" src="/static/more.png" mode="aspectFit" @click="changeOperate(item.id, true)"></image>
						<view class="operate-list">
							<view class="list-option delete" @click="handleDelete(item.id)">删除</view>
							<view class="list-option edit" @click="handleEdit(item.id)">编辑</view>
						</view>
						<view class="operate-mask" @click="changeOperate(item.id, false)"></view>
					</view>
				</view>
			</view>
			<empty top="30%" title="暂无相关内容~" v-else></empty>
		</view>
		<!-- 底部按钮 -->
		<view class="container-footer" @click="closeOperate()">
			<view class="footer-btn" @click="handleAdd()">添加乘车人</view>
			<view class="safe-padding"></view>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data() {
			return {
				// 页面是否阻止滚动
				pageShow: false,
				// 加载完成
				loadEnd: false,
				// 乘车人列表
				passengerList: [],
				// 打开操作弹窗索引
				openOperateIndex: -1,
				// 是否为选择功能
				isSelect: false,
				// 乘车人列表是否加载
				passengerLoading: false,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		onLoad(option) {
			uni.showLoading({
				title: "加载中"
			})
			this.isSelect = option.select == 1 ? true : false
			this.getPassenger(() => {
				uni.hideLoading()
				this.loadEnd = true
			})
		},
		onShow() {
			if (this.loadEnd) {
				this.passengerLoading = true
				this.getPassenger(() => {
					this.passengerLoading = false
				})
			}
		},
		onPullDownRefresh() {
			this.passengerLoading = true
			this.getPassenger(() => {
				this.passengerLoading = false
				uni.stopPullDownRefresh()
			})
		},
		methods: {
			// 获取乘车人列表
			getPassenger(fn) {
				this.$util.request("passenger.passenger.list").then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.openOperateIndex = -1
						this.passengerList = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取乘车人列表 ', error)
				})
			},
			// 切换操作选项打开状态
			changeOperate(id, state) {
				const index = this.passengerList.findIndex(item => item.id == id)
				this.passengerList[index].openOperate = state
				if (state) this.openOperateIndex = index
				else this.openOperateIndex = -1
				this.$forceUpdate()
			},
			// 关闭操作弹窗
			closeOperate() {
				if (this.openOperateIndex > -1) {
					this.passengerList[this.openOperateIndex].openOperate = false
					this.openOperateIndex = -1
				}
			},
			// 添加乘车人
			handleAdd() {
				this.$util.toPage({
					mode: 1,
					path: "/pages/passenger/mine/passenger/edit",
					isToken: true,
				})
			},
			// 编辑乘车人
			handleEdit(id) {
				this.closeOperate()
				this.$util.toPage({
					mode: 1,
					path: "/pages/passenger/mine/passenger/edit?id=" + id,
					isToken: true,
				})
			},
			// 删除乘车人
			handleDelete(id) {
				this.closeOperate()
				uni.showModal({
					content: " 确认删除此乘车人？ \n 点击【确认删除】按钮删除 ",
					confirmText: "确认删除",
					cancelText: "我再想想",
					confirmColor: "#DE2826",
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: "加载中",
								mask: true
							})
							this.$util.request("passenger.passenger.delete", { id }).then(res => {
								uni.hideLoading()
								if (res.code == 1) {
									uni.showToast({
										icon: "success",
										title: "删除成功"
									})
									this.passengerLoading = true
									this.getPassenger(() => {
										this.passengerLoading = false
									})
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							}).catch(error => {
								uni.hideLoading()
								console.error('删除紧急联系人 ', error)
							})
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		padding-bottom: 112rpx;

		.container-main {
			.main-list {
				padding: 32rpx;

				.list-item {
					border-radius: 16rpx;
					background: #FFF;
					margin-top: 32rpx;
					padding: 32rpx;

					&:first-child {
						margin-top: 0;
					}

					.item-mask {
						width: 100%;
						height: 44rpx;
						background: #eee;
						margin-right: 24rpx;
					}

					.item-box {
						.text {
							color: #242629;
							font-size: 32rpx;
							font-weight: 600;
							line-height: 44rpx;
							margin-right: 16rpx;
						}
					}

					.item-operate {
						position: relative;

						.operate-btn {
							width: 48rpx;
							height: 48rpx;
						}

						.operate-list {
							display: none;
							position: absolute;
							right: 0;
							top: 68rpx;
							width: 176rpx;
							border-radius: 12rpx;
							background: #FFF;
							box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, 0.10);
							z-index: 89;

							.list-option {
								font-size: 28rpx;
								line-height: 40rpx;
								text-align: center;
								padding: 24rpx;
								border-top: 1rpx solid #F6F7FB;

								&:first-child {
									border-top: none;
								}

								&.delete {
									color: #DE2826;
								}

								&.edit {
									color: #FFBD0A;
								}
							}
						}

						.operate-mask {
							display: none;
							position: fixed;
							top: 0;
							left: 0;
							right: 0;
							bottom: 0;
							z-index: 88;
						}

						&.expand {

							.operate-list,
							.operate-mask {
								display: block;
							}
						}
					}
				}
			}
		}

		.container-footer {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			z-index: 96;
			background: #ffffff;
			border-top: 1rpx solid #F6F7FB;
			padding: 16rpx 24rpx;

			.footer-btn {
				color: #ffffff;
				font-size: 28rpx;
				line-height: 40rpx;
				padding: 20rpx 24rpx;
				border-radius: 40rpx;
				background: var(--theme-color);
				text-align: center;
			}
		}
	}
</style>